package paquetes.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import paquetes.manager.AdministradorFacturas;
import paquetes.negocioVO.FacturaVO;
import paquetes.negocioVO.PedidoVO;


public class ControladorFacturas extends HttpServlet {

	private static final long serialVersionUID = 1087702007634924546L;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
            String action = request.getParameter("action");
            String jspPage = "/index.html";
            
            
            if(request.getParameter("cancelar4")!=null)
            {
            	action ="default";
            }
  
            else if(request.getParameter("show")!=null)
            {
            	action = "show";
            }

            if ((action == null) || (action.length() < 1))
            {
                action = "default";
            }

            if ("default".equals(action))
            {
                jspPage = "/index.html";
            }
            else if ("displayFacturas".equals(action))
            {
            	List<FacturaVO> facturas = new ArrayList<FacturaVO>();
            	facturas = AdministradorFacturas.getInstancia().getFacturas();
            	request.setAttribute("facturas", facturas);
				jspPage = "jsp/Factura/GenerarF.jsp";        
            }
            else if ("efectuarFactura".equals(action)){
            	int nro = Integer.valueOf(request.getParameter("listado"));
            	PedidoVO p = AdministradorFacturas.getInstancia().buscarPedido(nro);
            	AdministradorFacturas.getInstancia().generarFactura(p);
            }
            else if ("show".equals(action)){
            	 Integer nro = Integer.valueOf(request.getParameter("listado"));
            	 PedidoVO c = AdministradorFacturas.getInstancia().buscarPedido((long)nro); 
 				 request.setAttribute("remi", c);
                 jspPage = "jsp/Factura/MostrarRemito.jsp";
            }
            dispatch(jspPage, request, response);
        }
    
		protected void dispatch(String jsp, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
            if (jsp != null)
            {
                RequestDispatcher rd = request.getRequestDispatcher(jsp);
                rd.forward(request, response);
            }
        }

        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
        	System.out.println(request.toString());
            doPost(request, response);
        }
        
}
